<?php
/**
 * Created by PhpStorm.
 * User: ZhangWei
 * Date: 2017/3/28
 * Time: 16:21
 */

namespace App\Api\V1\Controllers;

use App\Models\Article\Comment;
use App\Models\Article\Post;
use App\User;
use Auth;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Tymon\JWTAuth\Facades\JWTAuth;

class UserController extends BaseController
{

    public function addComment(Request $request)
    {
        $messages = [
            'required' => '请输入:attribute',
        ];
        $v = Validator::make($request->all(), [
            'post_id' => 'required|int',
            'content' => 'required|string',
        ], $messages);
        if ($v->fails()) {
            return $this->response->array(['status_code' => 0, 'message' => $v->getMessageBag()->first(), 'data' => []]);
        }
        $post_id = $request->input("post_id");
        $post = Post::find($post_id);

        if (empty($post)) {
            return $this->response->array(['status_code' => 0, 'message' => "文章不存在", 'data' => []]);
        }

        $content = Comment::create([
            'post_id' => $post_id,
            'content' => $request->input("content"),
            'user_id' => Auth::id(),
        ]);
        return $this->response->array(['status_code' => 200, 'message' => '评论成功', 'data' => $content]);
    }


    public function getUser()
    {
        $user = Auth::user();

        return $this->response->array($user);
    }

    public function app_login(Request $request)
    {
        if (Auth::attempt(['email' => $request->input('email'), 'password' => $request->input('password')])) {
            $user = Auth::getUser();
            $token = JWTAuth::fromUser($user);

            $user['token'] = $token;

            return $this->response->array(['status_code' => 200, 'message' => '登录成功', 'data' => $user]);
        } else {
            return $this->response->array(['status_code' => 0, 'message' => '账号或密码错误']);
        }


    }

    public function register(Request $request)
    {
        $v = $this->validator($request->all());

        if ($v->fails()) {
            return $this->response->array(['status_code' => 0, 'message' => $v->getMessageBag()->first(), 'data' => []]);
        }

        event(new Registered($user = $this->create($request->all())));

        $this->guard()->login($user);

        $token = JWTAuth::fromUser($user);

        $user['token'] = $token;

        return $this->response->array(['status_code' => 200, 'message' => '注册成功', 'data' => $user]);

    }

    protected function guard()
    {
        return Auth::guard();
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        $messages = [
            'required' => '请输入:attribute',
            'email.unique' => '邮箱已存在',
            'confirmed' => '两次密码输入不一致'
        ];

        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',
        ], $messages);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }

    protected function validateLogin(Request $request)
    {
        $this->validate($request, [
            $this->username() => 'required|string',
            'password' => 'required|string',
        ]);
    }

    public function username()
    {
        return 'email';
    }
}